import request from '@/utils/request'

const SystemConfigAPI = {
  getList: function (type, params) {
    let op_id = ''
    switch (type) {
      case 'todo':
        op_id = 'getCartonTaskToDo'
        break
      case 'chart':
        op_id = 'getCartonChart'
        break
      case 'clientAmt':
        op_id = 'getCartonClientAmt'
        break
      case 'orderType':
        op_id = 'getCartonOrderType'
        break
      case 'statistics':
        op_id = 'getCartonStatistics'
        break
      case 'notice':
        op_id = 'getNotice'
        break
      case 'picture':
        op_id = 'getPicture'
        break
      case 'commonFunction':
        op_id = 'getCommonFunction'
        break
      case 'noticeDetail':
        op_id = 'noticeDetail'
        break
      default:
        break
    }
    params = Object.assign({ op_id }, params)
    return new Promise((resolve) => {
      const url = params.op_id
      request({
        url,
        method: 'get',
        params
      })
        .then((res) => {
          resolve(res)
        })
        .catch(() => {
          const data = {
            total: 0,
            pageItems: []
          }
          resolve({ data })
        })
    })
  },
  addOne: function (type, data) {
    let op_id = ''
    switch (type) {
      case 'insertCommonFunction':
        op_id = 'insertCommonFunction'
        break
    }
    const params = { op_id }
    // 过滤掉data中无效字段
    const tmp = {}
    for (const key of Object.keys(data)) {
      if (data[key] || data[key] === 0) {
        tmp[key] = data[key]
      }
    }
    return new Promise((resolve) => {
      const url = params.op_id
      request({
        url,
        method: 'post',
        params,
        data: tmp
      })
        .then(res => {
          resolve(res)
        })
        .catch(() => {
          const data = {
            total: 0,
            pageItems: []
          }
          resolve(data)
        })
    })
  }
}

export default SystemConfigAPI